﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model_Layer;
using System.Data.SqlClient;
using System.Transactions;

namespace Data_Access_Layer
{
    public class DBPassenger : IDBPassenger
    {
        private SqlConnection con;

        public DBPassenger()
        {
            con = Connection.GetInstance().GetConnection();
        }

        public void InsertPassenger(List<Passenger> passengerList)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    foreach (Passenger pas in passengerList)
                    {
                    con.Open();
                    getMaxId max = new getMaxId();
                    int maxId = max.maxId("select MAX(personid) FROM Person");
                    string query = "insert into Person values (" + maxId + "," + pas.Fname + "," + pas.Lname + "," + pas.Gender + "," + pas.Paddress + "," + pas.DateOfBirth + "," + pas.IsCusFlag + "," + pas.IsPassFlag + ")";
                    SqlCommand cmd = new SqlCommand(query, con);
                    cmd.ExecuteNonQuery();

                    query = "insert into Passenger values (" + maxId + "," + pas.PassportNo + ")";
                    cmd = new SqlCommand(query, con);
                    cmd.ExecuteNonQuery();
                    scope.Complete();
                    }
                }
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
    }
}
